package cn.iocoder.yudao.module.promotion.fod.controller.admin;

import cn.iocoder.yudao.framework.business.basic.annotation.AdminJsonView;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.api.promotion.fod.bo.FirstOrderDiscountBO;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.promotion.fod.service.FirstOrderDiscountConfigService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 前端控制器
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/09/07
 */
@Tag(name = "管理后台 - 首单折扣配置")
@RestController
@RequestMapping("/promotion/fod/config")
public class FirstOrderDiscountConfigController {
    @Resource
    private FirstOrderDiscountConfigService firstOrderDiscountConfigService;

    @Resource
    private ShopInfoApi shopInfoApi;

    @GetMapping("/read")
    @Operation(summary = "读取配置内容")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    @JsonView(AdminJsonView.class)
    public CommonResult<List<FirstOrderDiscountBO>> read() {
        Long shopId = shopInfoApi.getShopIdByAdminUserId(getLoginUserId());
        List<FirstOrderDiscountBO> list = firstOrderDiscountConfigService.getList(shopId);
        return success(list);
    }

    @PostMapping("/write")
    @Operation(summary = "写入配置内容")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> write(@RequestBody @Valid List<FirstOrderDiscountBO> param) {
        Long shopId = shopInfoApi.getShopIdByAdminUserId(getLoginUserId());
        return success(firstOrderDiscountConfigService.save(shopId, param));
    }

}
